package com.yunzheng.studyLeaveSystem.modules.enrollAndLogin.login.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunzheng.studyLeaveSystem.modules.enrollAndLogin.login.dao.UserDao;
import com.yunzheng.studyLeaveSystem.modules.enrollAndLogin.login.dao.UserDaoImpl;
import com.yunzheng.studyLeaveSystem.modules.enrollAndLogin.login.entity.Admin;
import com.yunzheng.studyLeaveSystem.modules.enrollAndLogin.login.service.UserServiceImpl;
import com.yunzheng.studyLeaveSystem.modules.response.ResponseWrapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileStore;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/01/30/10:53
 * @Description:登录功能与前端交互
 */

@WebServlet(name = "LoginServlet",value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    Admin admin = new Admin();
    UserServiceImpl userService = new UserServiceImpl();

    UserDao userDao = new UserDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取请求体数据
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        //将JSON字符转化为JAVA对象
        JSONObject jsonObject = JSON.parseObject(readLine);
        String phone_num = jsonObject.getString("phone_num");
        String password = jsonObject.getString("password");

        HttpSession session = request.getSession();

        try {
            if (phone_num.equals(admin.getAdminPhone())  && password.equals(admin.getAdminPwd())){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.ADMIN_SUCCESS();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
               
            }
            else if (userService.isLogin(phone_num,password)){
                ResponseWrapper responseWrapper = null;
                Integer id = userDao.queryId(phone_num);
                session.setAttribute("id", id);
                responseWrapper = ResponseWrapper.USER_SUCCESS();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
               
            }
            else if(!userService.isExist(phone_num) && !phone_num.equals(admin.getAdminPhone())){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.ACCOUNT_FAILURE();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
               
            }
            else if (!userService.isLogin(phone_num,password) && !password.equals(admin.getAdminPwd())) {
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.PWD_FAILURE();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

